define('framework/databind/GillionDataBindDirectiveConstructor', ['angular'], function (angular) {

    return function ($compile, $parse, $timeout, $filter) {
        return {
            restrict: 'A',
            scope: {
                gDataBind: '=',
                ngModelInit:'=ngModel',
                valueProp:'@'

            },
            require: '^?ngModel',
            compile:function (tElement, tAttrs) {
                return function (scope, element, attrs, ngModel) {
                    scope.valuePropGetter = $parse(scope.valueProp);
                    scope.valueSeparator = attrs.valueSeparator || ',';//分隔符
                    scope.cascade = attrs.cascade=="true" || true;//是否级联
                    scope.multi = attrs.multi == "true" || false;//是否多选

                    var valueText = '',
                        submitValue,
                        isHidden = element.attr("type")=='hidden';
                    if(scope.multi){
                        //有初始值
                        scope.$watchCollection("gDataBind",function(){
                            if(scope.gDataBind){
                                var init =true;
                                //判断是否已经构造好对象（初始化时 需要构造对象）
                                angular.forEach(scope.gDataBind,function(item){
                                    if(item[scope.valueProp]!=undefined){
                                        init = false
                                    }
                                });
                                if(init && scope.ngModelInit && !isHidden){
                                    var ngModelArr = scope.ngModelInit.split(scope.valueSeparator);
                                    for(var i=0;i<ngModelArr.length;i++){
                                        if(!scope.gDataBind[i]){
                                            scope.gDataBind[i] ={};
                                        }
                                        scope.gDataBind[i][scope.valueProp]=ngModelArr[i];
                                    }
                                }else{
                                    submitValue = [];

                                    angular.forEach(scope.gDataBind,function(item){
                                        submitValue.push(scope.valuePropGetter(item));
                                    });
                                    ngModel.$setViewValue(submitValue.join(scope.valueSeparator));
                                    ngModel.$render();
                                }
                            }

                        });
                    }else{
                        var init = true;//第一次初始化要赋值
                        scope.$watch("gDataBind",function(){
                            if(scope.valuePropGetter(scope.gDataBind)!=undefined){
                                ngModel.$setViewValue(scope.valuePropGetter(scope.gDataBind));
                                ngModel.$render();
								if(scope.ngModelInit){
                                    init = false;
                                }
                            }else{
                                if(scope.gDataBind && init){
                                    scope.gDataBind[scope.valueProp] = scope.ngModelInit;
                                    init = false;
                                }else{
                                    ngModel.$setViewValue('');
                                    ngModel.$render();
                                }
                            }
                        });
                    }

                    scope.dupRemoval= function(rowDatas){

                        var result = [], hash = {},value;
                        for (var i = 0, elem; (elem = rowDatas[i]) != null; i++) {
                            value = scope.valuePropGetter(elem);
                            if (!hash[value]) {
                                result.push(elem);
                                hash[value] = true;
                            }
                        }
                        return result;
                    };
                    if(scope.cascade){
                        element.on('keyup', function (event) {
                            var e = e || event,
                                currKey = e.keyCode || e.which || e.charCode,
                                inputVal,keywordArr = [],selectedRow=[];

                            switch (currKey) {
                                //删除时触发
                                case 8:
                                    inputVal = element.val();
                                    if(inputVal){
                                        if(angular.isArray(scope.gDataBind)){
                                            keywordArr = inputVal.split(scope.valueSeparator);
                                            angular.forEach(keywordArr,function(each){
                                                angular.forEach(scope.gDataBind,function(item){
                                                    if(scope.valuePropGetter(item)==each){
                                                        selectedRow.push(item);
                                                    }
                                                })
                                            });
                                            angular.forEach(keywordArr,function(each){
                                                angular.forEach(scope.gDataBind,function(item){
                                                    if(scope.valuePropGetter(item)==each){
                                                        selectedRow.push(item);
                                                    }
                                                })
                                            });
                                            selectedRow = scope.dupRemoval(selectedRow);
                                            scope.gDataBind = selectedRow;
                                        }else{
                                            if(scope.valuePropGetter(scope.gDataBind)!=inputVal){
                                                scope.gDataBind = undefined;
                                            }
                                        }
                                    }else{
                                        if(angular.isArray(scope.gDataBind)){
                                            scope.gDataBind = [];
                                        }else{
                                            scope.gDataBind = undefined;
                                        }
                                    }
                                    if(!scope.$root.$$phase){
                                        scope.$apply();
                                    }
                                    break;
                                default :
                                    break;
                            }
                        });
                    }
                }
            }
        };
    };
});